package com.cloudeasy.post.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudeasy.api.util.EntityUtil;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.post.PostVO;
import com.cloudeasy.api.vo.post.SimpleUserInfoVO;
import com.cloudeasy.post.field.PostField;
import com.cloudeasy.post.po.Post;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 帖子工具类
 */
public class PostUtil {
    /**
     * 构建查询
     *
     * @param area 区域
     * @param type 类型
     * @param asc  正序排序字段
     * @param desc 倒序排序字段
     * @return {@link QueryWrapper<Post>}
     */
    public static QueryWrapper<Post> compose(String area, Integer type, String[] asc, String[] desc) {
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("area", area);
        if (type != null) {
            queryWrapper.eq("type", type);
        }
        if (asc != null && asc.length > 0) {
            EntityUtil.checkParamter(PostField.getFieldList(), asc);
            queryWrapper.orderByAsc(asc);
        }
        if (desc != null && desc.length > 0) {
            EntityUtil.checkParamter(PostField.getFieldList(), desc);
            queryWrapper.orderByDesc(desc);
        }
        return queryWrapper;
    }

    /**
     * 转换VO对象
     *
     * @param post                {@link Post}
     * @param simpleUserInfoVOMap {@link Map<String, SimpleUserInfoVO>}
     * @return {@link PostVO}
     */
    public static PostVO convert(Post post, Map<String, SimpleUserInfoVO> simpleUserInfoVOMap) {
        PostVO postVO = new PostVO();
        BeanUtils.copyProperties(post, postVO);
        postVO.setAuthor(simpleUserInfoVOMap.get(post.getAuthorId()));
        return postVO;
    }

    /**
     * 转换VO对象
     *
     * @param posts             {@link List<Post>}
     * @param simpleUserInfoVOS {@link List<SimpleUserInfoVO>}
     * @return {@link List<PostVO>}
     */
    public static List<PostVO> convert(List<Post> posts, List<SimpleUserInfoVO> simpleUserInfoVOS) {
        Map<String, SimpleUserInfoVO> simpleUserInfoVOMap = toMap(simpleUserInfoVOS);
        return posts.stream().map(post -> convert(post, simpleUserInfoVOMap)).collect(Collectors.toList());
    }

    /**
     * 转换VO对象
     *
     * @param postIPage {@link IPage<Post>}
     * @param simpleUserInfoVOS {@link List<SimpleUserInfoVO>}
     * @return {@link PageResult<PostVO>}
     */
    public static PageResult<PostVO> convert(IPage<Post> postIPage, List<SimpleUserInfoVO> simpleUserInfoVOS) {
        Map<String, SimpleUserInfoVO> simpleUserInfoVOMap = toMap(simpleUserInfoVOS);
        PageResult<PostVO> postVOPageResult = new PageResult<>();
        postVOPageResult.setTotal(postIPage.getTotal())
                .setPages(postIPage.getPages())
                .setRecords(postIPage.getRecords().stream().map(post -> convert(post, simpleUserInfoVOMap)).collect(Collectors.toList()));
        return postVOPageResult;
    }

    /**
     * 将简单用户信息列表转化为Id为key的Map
     *
     * @param simpleUserInfoVOS {@link List<SimpleUserInfoVO>}
     * @return {@link Map<String, SimpleUserInfoVO>}
     */
    private static Map<String, SimpleUserInfoVO> toMap(List<SimpleUserInfoVO> simpleUserInfoVOS) {
        return simpleUserInfoVOS.stream().collect(Collectors.toMap(SimpleUserInfoVO::getId, simpleUserInfoVO -> simpleUserInfoVO, (o, o2) -> o));
    }
}
